package Day017_缓冲流_转换流_序列化流_打印流.demo4_序列流.Test5_序列化集合;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 * @author zhangyan
 * @date 2020/5/9 14:43
 */
public class Test {
    public static void main(String[] args) throws Exception {
        //序列化集合
        //1.创建学生对像
        Student stu1 = new Student("张三", 18);
        Student stu2 = new Student("李四", 28);
        Student stu3 = new Student("王五", 20);
        Student stu4 = new Student("赵六", 22);
        Student stu5 = new Student("吴七", 19);

        //2.创建集合
        ArrayList<Student> list = new ArrayList<>();
        //3.添加对象到集合
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        list.add(stu4);
        list.add(stu5);
        //4.创建序列化流对象
        FileOutputStream fos = new FileOutputStream("Demo02\\aaa\\list.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        //5.序列化集合
        oos.writeObject(list);
        //6.关流
        oos.close();
        //-----------------------------------------------------------------------------------

        //反序列化集合
        //
        FileInputStream fis = new FileInputStream("Demo02\\aaa\\list.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        //重构集合对象
        ArrayList<Student> list1 = (ArrayList<Student>) ois.readObject();
        //关流
        ois.close();
        //打印集合
        System.out.println(list1);

    }
}
